package com.ztools.wx.handler;

import com.ztools.wx.async.WxMpAsyncTasks;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author:seeTools
 * @Date: 2023/4/8  21:35
 * @Version 1.0
 */
@AllArgsConstructor
@Component
public class SubscribeHandler extends AbstractHandler {

    private final WxMpAsyncTasks wxMpAsyncTasks;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        logger.info("新关注用户 OPENID: " + wxMpXmlMessage.getFromUser());
        WxMpUser wxMpUser = wxMpService.getUserService().userInfo(wxMpXmlMessage.getFromUser());
        if (wxMpUser != null) {
            logger.info("新关注用户信息:{}", wxMpUser);
            wxMpAsyncTasks.subscribe(wxMpUser);
        }
        return null;
    }

}
